package com.blast.manager.pop

import android.content.Context
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.widget.AdapterView
import com.blast.manager.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.wxl.common.base.AbsActivity
import com.wxl.common.util.ObservableUtil
import com.wxl.common.util.Toll
import kotlinx.android.synthetic.main.pop_sort_layout.view.*

class DelaySortPop : CenterPopupView {

    private lateinit var callback: ObservableUtil.Companion.OnNextCallback<Int>
    private var p = -1
    private var c = -1
    private val keyDownView = arrayListOf<View>()
    private var selectIndex = 0

    constructor(context: Context) : super(context)

    override fun getImplLayoutId(): Int {
        return R.layout.pop_sort_layout
    }

    override fun onCreate() {
        super.onCreate()
        val styleArr = context.resources.getStringArray(R.array.sort_style)
        val typeArr = context.resources.getStringArray(R.array.sort_type)
        delaySortParentText.show(styleArr.asList(), AdapterView.OnItemClickListener { parent, view, position, id ->
            p = position
        })
        delaySortChildText.show(typeArr.asList(), AdapterView.OnItemClickListener { parent, view, position, id ->
            c = position
        })
//        delaySortParentLayout.setOnClickListener {
//            BlastAttachListPopupView.show(it, OnSelectListener { position, text ->
//                p = position
//                delaySortParentText.setText(text)
//            },*styleArr)
//        }
//        delaySortChildLayout.setOnClickListener {
//            BlastAttachListPopupView.show(it, OnSelectListener { position, text ->
//                c = position
//                delaySortChildText.setText(text)
//            }, *typeArr)
//        }

        delaySortCancel.setOnClickListener {
            dismiss()
        }
        delaySortSureBtn.setOnClickListener {
            if (TextUtils.isEmpty(delaySortParentText.text)) {
                Toll.toll("请选择排序方式")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(delaySortChildText.text)) {
                Toll.toll("请选择排序次式")
                return@setOnClickListener
            }
            dismiss()
            if (p == 0 && c == 0) {
                callback.onNext(SORT_HOLE_UP_CODE)
            }
            if (p == 0 && c == 1) {
                callback.onNext(SORT_HOLE_DOWN_DODE)
            }
            if (p == 1 && c == 0) {
                callback.onNext(SORT_DELAY_UP_CODE)
            }
            if (p == 1 && c == 1) {
                callback.onNext(SORT_DELAY_DOWN_CODE)
            }
            if (p == 2 && c == 0) {
                callback.onNext(SORT_NUM_UP_CODE)
            }
            if (p == 2 && c == 1) {
                callback.onNext(SORT_NUM_DOWN_CODE)
            }
        }

        keyDownView.clear()
        keyDownView.add(delaySortParentText)
        keyDownView.add(delaySortChildText)
        keyDownView.add(delaySortCancel)
        keyDownView.add(delaySortSureBtn)
        delaySortParentText.isFocusable = true
        delaySortParentText.isEnabled = true
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean{
        if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
            notifyFocusableChanged(true)
        }
        if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
            notifyFocusableChanged(false)
        }
        if(keyCode == KeyEvent.KEYCODE_BACK){
            dismiss()
        }
//        if(keyCode == KeyEvent.KEYCODE_ENTER){
//            delaySortSureBtn.performClick()
//        }
        return true
    }

    private fun notifyFocusableChanged(isDown:Boolean){
        var p = 0
        keyDownView.forEach {
           if(p == selectIndex){
               if(isDown){
                   if(p <  keyDownView.size - 1){
                       it.isEnabled = false
                       it.isSelected = false
                       ++p
                       selectIndex = p
                       keyDownView[p].isFocusable = true
                       keyDownView[p].isEnabled = true
                       keyDownView[p].isSelected = true
                       keyDownView[p].requestFocus()
                   }
               } else {
                   if(p > 0){
                       it.isEnabled = false
                       it.isSelected = false
                       --p
                       selectIndex = p
                       keyDownView[p].isFocusable = true
                       keyDownView[p].isEnabled = true
                       keyDownView[p].isSelected = true
                       keyDownView[p].requestFocus()
                   }
               }

               return
           }
            p++
        }
    }


    companion object {

        const val SORT_HOLE_UP_CODE = 1
        const val SORT_HOLE_DOWN_DODE = 2
        const val SORT_DELAY_UP_CODE = 3
        const val SORT_DELAY_DOWN_CODE = 4
        const val SORT_NUM_UP_CODE = 5
        const val SORT_NUM_DOWN_CODE = 6

        fun show(context: Context, callback: ObservableUtil.Companion.OnNextCallback<Int>) {
            val pop = DelaySortPop(context)
            pop.callback = callback
            XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .asCustom(pop)
                .show()
            (context as AbsActivity).addPop(pop)
        }
    }
}